package com.example.zyfypt_611cgt.model;

import com.example.zyfypt_611cgt.bean.ArticleBean;
import com.example.zyfypt_611cgt.bean.ProjectBean;
import com.example.zyfypt_611cgt.common.Common;
import com.example.zyfypt_611cgt.iface.ArticleListener;
import com.example.zyfypt_611cgt.iface.ProjectListener;
import com.example.zyfypt_611cgt.iface.Projectiface;
import com.example.zyfypt_611cgt.iface.Tcaseiface;
import com.example.zyfypt_611cgt.service.ArticleService;
import com.example.zyfypt_611cgt.service.ProjectService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProjectModel implements Projectiface {
    private Retrofit retrofit;
    //构造函数
    public ProjectModel()
    {//使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String session, String mod, int page, ProjectListener listener) {
        //retrofit使用的5-2小步
        ProjectService service = retrofit.create(ProjectService.class);
        Call<List<ProjectBean>> call = service.getProjectList(session, mod, page);
        //5-3
        call.enqueue(new Callback<List<ProjectBean>>() {
            @Override
            public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                if(response.isSuccessful() && response.body() != null){
                    listener.onResponse(response.body());
                } else{
                    listener.onFail("on response fail");
                }
            }

            @Override
            public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
